package cs341.group2.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import cs341.group2.mycontacts.Database.Category;
import cs341.group2.mycontacts.Database.Contact;
import cs341.group2.mycontacts.Database.MyContactsDB;

public class Search extends ListActivity {
	private EditText searchBox = null;
	private ContactButtonAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        
        searchBox = (EditText)findViewById(R.id.SearchContactName);
        /*searchBox.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(MotionEvent.ACTION_UP == event.getAction()) {
					//searchBox.setText("");
				}
				return true;
			}*/
		//});
        searchBox.addTextChangedListener(searchTextWatcher);
        
        Button b2=(Button)findViewById(R.id.button_contact);
        b2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i2 = new Intent(Search.this, cs341.group2.mycontacts.ContactPage.class);
				i2.putExtra("state", "add");
				startActivity(i2);
			}
		});
        
        Button b=(Button)findViewById(R.id.button_groups);
		 b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Search.this, cs341.group2.mycontacts.MainActivity.class);
				startActivity(i);
			}
		});
        
    }
    
    private TextWatcher searchTextWatcher = new TextWatcher() {
        @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
        		adapter.getFilter().filter(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // ignore
            }

            @Override
            public void afterTextChanged(Editable s) {
                // ignore
            }
        };
    
    public void PopulateContactsList(Contact[] contacts){
    	 ListView list = (ListView)findViewById(android.R.id.list);
		 list.setClickable(true);
		 ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		 HashMap<String, String> map;
		 MyContactsDB database = new MyContactsDB(this);
		 for(int i = 0; i < contacts.length; i++){
		     map = new HashMap<String, String>();
		     map.put("contactName", contacts[i].Name);
		     map.put("contactId", String.valueOf(contacts[i].Id));
		     listItem.add(map);
		 }
		 adapter = new ContactButtonAdapter (this.getBaseContext(), listItem);
		 list.setAdapter(adapter);
		 
    }
    
    @Override
	public void onResume() {
		super.onResume(); // Always call the superclass method first

		new GetAllContacts().execute((Object[]) null);
	}
    
    private class GetAllContacts extends
	AsyncTask<Object, Object, Contact[]> {
		public MyContactsDB myContactsDb = new MyContactsDB(Search.this);
		
		@Override
		protected Contact[] doInBackground(Object... params) {
			Contact contact = new Contact();
			myContactsDb.open();
			//myContactsDb.insertContact(contact);
			return myContactsDb.getAllContacts();
		}

		@Override
		protected void onPostExecute(Contact[] contacts) {
			myContactsDb.close();
			PopulateContactsList(contacts);
		}
	}
    
      

}
